<template>
  <div class="page has-navbar" v-nav="{ title: '购物车', showBackButton: true, onBackButtonClick: back }">
    <div class="page-content text-center" id="ul">
      <div class="list">
        <div class="item" f-id="1" @touchmove="touchLeft" @touchstart="touchstart" @touchend="touchend">
          <a class="icon" @click.stop="selectFlower" f-id="1"></a>
          <img src="../image/1.png" alt="" class="img">
          <div>
            <h1 class="title" >满天星鲜花束 1束</h1>
            <div class="price">
              <span class="hb miniFont">￥</span><span class="priceNumber">415</span><span class="miniFont">.50</span>
            </div>
            <div class="xxqk">
              <span>规格：16枝</span> |
              <span>颜色：红色</span>
            </div>
          </div>
          <aside class="count">x2</aside>
        </div>
        <div class="delBtn" @click="delFlower" f-id="1">删除</div>
      </div>
      <div class="list">
        <div class="item" f-id="2" @touchmove="touchLeft" @touchstart="touchstart" @touchend="touchend">
          <a class="icon" @click.stop="selectFlower" f-id="2"></a>
          <img src="../image/1.png" alt="" class="img">
          <div>
            <h1 class="title" >满天星鲜花束 1束</h1>
            <div class="price">
              <span class="hb miniFont">￥</span><span class="priceNumber">415</span><span class="miniFont">.50</span>
            </div>
            <div class="xxqk">
              <span>规格：16枝</span> |
              <span>颜色：红色</span>
            </div>
          </div>
          <aside class="count">x2</aside>
        </div>
        <div class="delBtn" @click="delFlower" f-id="2">删除</div>
      </div>
      <div class="list">
        <div class="item" f-id="3" @touchmove="touchLeft" @touchstart="touchstart" @touchend="touchend">
          <a class="icon" @click.stop="selectFlower" f-id="3"></a>
          <img src="../image/1.png" alt="" class="img">
          <div>
            <h1 class="title" >满天星鲜花束 1束</h1>
            <div class="price">
              <span class="hb miniFont">￥</span><span class="priceNumber">415</span><span class="miniFont">.50</span>
            </div>
            <div class="xxqk">
              <span>规格：16枝</span> |
              <span>颜色：红色</span>
            </div>
          </div>
          <aside class="count">x2</aside>
        </div>
        <div class="delBtn" @click="delFlower" f-id="3">删除</div>
      </div>
    </div>
    <div class="buttonMenu">
      <div class="kefu">
        <p>
          <span>￥880.00</span> |
          <span>不含配送费</span>
        </p>
      </div>
      <div class="buy">
        去支付
      </div>
    </div>
  </div>
</template>

<script>
  import ArrUtil from '../util/arr.js'
    export default {
      name: "Cart",
      data(){
        return{
          touchStartX : 0,
          touchEndX : 0,
          touchMoveX : 0,
          flowerIds : []
        }
      },
      methods : {
        back() {
          this.$router.go(-1)
        },
        touchLeft(ev){
          this.touchMoveX= ev.targetTouches[0].clientX;
          let tag = ev.currentTarget;
          let delBtn = tag.nextElementSibling;
          let moveLen = this.touchStartX - this.touchMoveX;
          if (moveLen > 0){
            tag.style.transform = `translateX(-100px)`;
            delBtn.style.transform = `translateX(-100px)`;
          }
          if (moveLen < 0){
            tag.style.transform = `translateX(0px)`;
            delBtn.style.transform = `translateX(0px)`;
          }
        },
        touchstart(ev){
          this.touchStartX = ev.targetTouches[0].clientX;
        },
        touchend(ev){
          this.touchEndX = ev.changedTouches[0].clientX;
        },
        selectFlower(ev){
          let tag = ev.currentTarget;
          let selectedId = tag.getAttribute('f-id');
          if (!tag.hasChildNodes()) {
            let $icon = document.createElement('i');
            $icon.setAttribute('class' , 'icon ion-android-checkmark-circle');
            tag.appendChild($icon);
            tag.style.border = 'none';
            this.flowerIds.push(selectedId);//添加选中的项目id

          }else {
            tag.innerHTML = '';
            tag.style.border = '1px solid #999';
            let index = ArrUtil.isRepeat(this.flowerIds , selectedId);
            this.flowerIds.splice(index , 1);
          }
          console.log(this.flowerIds);
        },
        delFlower(ev){
          let tagFId = ev.currentTarget.getAttribute('f-id');
          let $ul = document.getElementById('ul');
          let $list = document.getElementsByClassName('list');
          // console.log($list[0].childNodes[0] , $list);
          for (let i = 0; i < $list.length; i++) {
            let $listChild = $list[i].childNodes[0];
            let fId = $listChild.getAttribute('f-id');
            if (tagFId === fId){
              console.log($ul);
              $ul.removeChild($list[i]);
            }
          }
        },
      }
    }
</script>

<style scoped lang="less">
  @priceColor : #ff3300;
  .list {
    position: relative;
    overflow: hidden;
    height: 108px;
    background: white;
    margin: 0;
    img.img{
      width: 70px;
      height: 70px;
      margin-right: 15px;
      margin-left: 30px;
    }
    .item {
      width: 100%;
      height: 108px;
      background: none;
      position: absolute;
      left: 0;
      transition: .4s;
      display: flex;
      text-align: start;
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
      .icon{
        width: 24px;
        height: 24px;
        margin-right: 5px;
        font-size: 24px;
        border: 1px solid #999999;
        -webkit-border-radius: 50%;
        -moz-border-radius: 50%;
        border-radius: 50%;
        position: absolute;
        top: calc(50% - 12px);
      }
      .title {
        font-size: 18px;
        margin-bottom: 0;
      }
      //价格
      .price{
        color: @priceColor;
        line-height: 32px;
        .priceNumber{
          font-size: 18px;
        }
        .miniFont{
          font-size: 12px;
        }
      }
      //详细情况
      .xxqk{
        display: flex;
        justify-content: space-between;
        color: #999999;
        font-size: 12px;
        img{
          margin: 0;
          width: 12px;
          height: 12px;
        }
      }
      //数量
      .count{
        position: absolute;
        right: 10px;
        font-size: 18px;
        top: calc(50% - 9px);
        color: #999999;
      }
    }
    .delBtn{
      width: 100px;
      background: #ff3300;
      min-height: 108px;
      line-height: 108px;
      position: absolute;
      top: 0;
      right: -100px;
      transition: .4s;
      color: white;
    }
  }
  .buttonMenu{
    position: absolute;
    bottom: 0;
    z-index: 999;
    width: 100%;
    display: flex;
    justify-content: space-between;
    font-size: 18px;
    text-align: center;
    .kefu{
      width: 60%;
      background: #ffffff;
      line-height: 0;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    .buy{
      background: #ff3300;
      width: 40%;
      color: #ffffff;
      font-weight: bold;
      line-height: 50px;
    }
    .buy{
      background: #ef2b67;
    }
  }
</style>
